package net.caixiaomi.info.ui.main;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sobot.chat.utils.ScreenUtils;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.ArticleAdapter;
import net.caixiaomi.info.adapter.DiscoveryGridAdapter;
import net.caixiaomi.info.adapter.DiscoveryScorerListAdapter;
import net.caixiaomi.info.adapter.HomeGridAdapter;
import net.caixiaomi.info.app.CommonApp;
import net.caixiaomi.info.base.BaseFragment;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.DiscoveryHallEntity;
import net.caixiaomi.info.model.DiscoveryHomePageEntity;
import net.caixiaomi.info.model.DiscoveryHotLeagueEntity;
import net.caixiaomi.info.model.DiscoveryScorerEntity;
import net.caixiaomi.info.model.DiscoveryScorerMemberEntity;
import net.caixiaomi.info.model.HomeModel;
import net.caixiaomi.info.model.HomePlayClassifyItem;
import net.caixiaomi.info.model.OpenPrizeEntity;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.ui.discovery.LeagueDetailActivity;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.info.util.SchemeUtil;
import net.caixiaomi.info.util.ToastUtil;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;

/**
 * Created by mac on 18/9/4.
 */

public class DiscoveryFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, OnRefreshListener {

    public static final String TAG = "DiscoveryFragment";

    @BindView(R.id.title)
    TextView mTitle;

    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;

    @BindView(android.R.id.list)
    RecyclerView mListView;

    @BindView(R.id.progress)
    CircularProgressBar mProgress;

    private View headerView;

    private RecyclerView mGridView0;
    private DiscoveryGridAdapter mGridViewAdapter0;

    private RecyclerView mGridView1;
    private DiscoveryGridAdapter mGridViewAdapter1;

    private LinearLayout mScorerLayout;

    private DiscoveryScorerListAdapter mAdapter;

    private int mCurrentScorer=0;

    private List<TextView> mTvList;

    private List<DiscoveryScorerMemberEntity> mTopList;

//    private RecyclerView mScorerListView;
//    private DiscoveryScorerListAdapter mScorerAdapter;

    public static DiscoveryFragment newInstance() {
        return new DiscoveryFragment();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        loadData();
    }

    @Override
    protected int fragmentLayout() {
        return R.layout.discovery_main_layout;
    }

    @Override
    protected void onFragmentCreated(View view, @Nullable Bundle savedInstanceState) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(getString(R.string.app_name)).append(" · ").append(getString(R.string.bottom_find));
        mTitle.setText(stringBuilder.toString());
        mRefresh.setOnRefreshListener(this);
        mTvList=new ArrayList<>();
        mTopList=new ArrayList<>();
        initView();
        loadData();
    }

    private void initView() {
        try {
            headerView = LayoutInflater.from(getActivity()).inflate(R.layout.dicsovery_header, null);
            mGridView0 = (RecyclerView) headerView.findViewById(R.id.grid0);
            mGridView0.setLayoutManager(new GridLayoutManager(getActivity(), 4));
            mGridViewAdapter0 = new DiscoveryGridAdapter(R.layout.discovery_main_select_item);
            mGridView0.setAdapter(mGridViewAdapter0);

            mGridView1 = (RecyclerView) headerView.findViewById(R.id.grid1);
            mGridView1.setLayoutManager(new GridLayoutManager(getActivity(), 4));
            mGridViewAdapter1 = new DiscoveryGridAdapter(R.layout.discovery_main_select_item);
            mGridView1.setAdapter(mGridViewAdapter1);

            mListView.setLayoutManager(new LinearLayoutManager(getActivity()));
            mAdapter = new DiscoveryScorerListAdapter(R.layout.discovery_scorer_item);
            mAdapter.setOnItemClickListener(this);
            mAdapter.addHeaderView(headerView);
            mAdapter.setEnableLoadMore(true);
            mListView.setAdapter(mAdapter);
            View scorerHeader=View.inflate(getActivity(),R.layout.discovery_scorer_item,null);
            scorerHeader.findViewById(R.id.discovery_scorer_padding).setVisibility(View.GONE);
            mAdapter.addHeaderView(scorerHeader);

            mScorerLayout=headerView.findViewById(R.id.discover_scorer_group);
            headerView.setVisibility(View.GONE);
        }catch (Exception e){

        }
    }

    private void bindData(final DiscoveryHomePageEntity entity){
        try {
            List<MultiItemEntity>list0=new ArrayList<>();
            list0.addAll(entity.getDiscoveryHallClassifyList());
            mGridViewAdapter0.setNewData(list0);
            mGridViewAdapter0.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    try {
                        DiscoveryHallEntity hall=entity.getDiscoveryHallClassifyList().get(position);
                        if(hall.getStatus()==0){    //上线
                            SchemeUtil.parseIntentUrl(getActivity(), hall.getRedirectUrl(),"");
                        }else {
                            ToastUtil.s(hall.getStatusReason());
                        }
                    }catch (Exception e){

                    }

                }
            });

            List<MultiItemEntity>list1=new ArrayList<>();
            list1.addAll(entity.getHotLeagueList());
            mGridViewAdapter1.setNewData(list1);
            mGridViewAdapter1.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    DiscoveryHotLeagueEntity league= (DiscoveryHotLeagueEntity) adapter.getData().get(position);
                    Intent intent=new Intent(getActivity(), LeagueDetailActivity.class);
                    intent.putExtra("leagueId",league.getLeagueId());
                    startActivity(intent);
                }
            });

            mScorerLayout.removeAllViews();
            mTvList.clear();
            for(int i=0;i<entity.getTopScorerDTOList().size();i++){
                DiscoveryScorerEntity scorer=entity.getTopScorerDTOList().get(i);
                View view=View.inflate(getActivity(),R.layout.des_item_layout,null);
                TextView tv=view.findViewById(R.id.item_des_tv);
//                LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(CommonApp.mScreenWidth/5,ViewGroup.LayoutParams.WRAP_CONTENT);
//                view.setLayoutParams(params);
                tv.setText(scorer.getLeagueName()+"-射手榜");
                mScorerLayout.addView(view);
                final int index=i;
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mCurrentScorer=index;
//                        List<DiscoveryScorerMemberEntity>list=mAdapter.getData();
//                        for(int i=0;i<list.size();i++){
//                            DiscoveryScorerMemberEntity ee=list.get(i);
//                            if(i<entity.getTopScorerDTOList().get(mCurrentScorer).getLeagueShooterInfoList().size()){
//                                DiscoveryScorerMemberEntity tar=entity.getTopScorerDTOList().get(mCurrentScorer).getLeagueShooterInfoList().get(i);
//                                ee.setInNum(tar.getInNum());
//                                ee.setMatchSeasonId(tar.getMatchSeasonId());
//                                ee.setPlayerName(tar.getPlayerName());
//                                ee.setPlayerTeam(tar.getPlayerTeam());
//                                ee.setSort(tar.getSort());
//                            }
//                        }

                        mTopList.clear();
                        mTopList.addAll(entity.getTopScorerDTOList().get(mCurrentScorer).getLeagueShooterInfoList());
//                        mAdapter.notifyDataSetChanged();
                        mAdapter.notifyItemChanged(1);
                        mAdapter.notifyItemChanged(2);
                        mAdapter.notifyItemChanged(3);
                        mAdapter.notifyItemChanged(4);
                        mAdapter.notifyItemChanged(5);
//                        mAdapter.setNewData(entity.getTopScorerDTOList().get(mCurrentScorer).getLeagueShooterInfoList());
                        for(int i=0;i<mTvList.size();i++){
                            mTvList.get(i).setTextColor(getResources().getColor(R.color.secondary_text));
                        }
                        if(mTvList.size()>mCurrentScorer){
                            mTvList.get(mCurrentScorer).setTextColor(getResources().getColor(R.color.orange_primary));
                        }
                    }
                });

                mTvList.add(tv);
                entity.getTopScorerDTOList().get(i).getLeagueShooterInfoList().get(entity.getTopScorerDTOList().get(i).getLeagueShooterInfoList().size()-1).setLast(true);
            }
            mTopList.addAll(entity.getTopScorerDTOList().get(mCurrentScorer).getLeagueShooterInfoList());
            mAdapter.setNewData(mTopList);
            for(int i=0;i<mTvList.size();i++){
                mTvList.get(i).setTextColor(getResources().getColor(R.color.secondary_text));
            }
            if(mTvList.size()>mCurrentScorer){
                mTvList.get(mCurrentScorer).setTextColor(getResources().getColor(R.color.orange_primary));
            }
        }catch (Exception e){

        }
    }



    private void loadData() {
        JSONObject params = new JSONObject();
        params.put("emptyStr", "");
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<DiscoveryHomePageEntity>> dis = requestService.getDiscoveryHomePage(params);
        dis.enqueue(new ResponseCallback<BaseCallModel<DiscoveryHomePageEntity>>() {
            @Override
            public void onSuccess(BaseCallModel<DiscoveryHomePageEntity> data) {
                mProgress.setVisibility(View.GONE);
                headerView.setVisibility(View.VISIBLE);
                mRefresh.finishRefresh();
                mTopList.clear();
                bindData(data.data);
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }
        });

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

}
